package com.ruoyi.match.job.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.match.constant.CommonConstants;
import com.ruoyi.match.job.service.RealTimeMatchTaskService;
import com.ruoyi.match.match.domain.RealTimeMatch;
import com.ruoyi.match.match.mapper.RealTimeMatchMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @author sjz
 * @version 1.0.0
 * @desc
 * @date 2023/11/5 15:15
 **/

@Slf4j
@RequiredArgsConstructor
@Service
public class RealTimeMatchTaskServiceImpl implements RealTimeMatchTaskService {

    private final RealTimeMatchMapper mapper;

    @Override
    public void getRealTimeMatchInfo(WebDriver browser, LocalDate startDate) throws Exception {
        String startTime = startDate.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
        String matchUrl = String.format(CommonConstants.MATCH_ODDS_URL, startTime);

        browser.get(matchUrl);

        String title = browser.getTitle();
        if("405".equals(title)){
            Thread.sleep(1000);
            browser.navigate().refresh();
            title = browser.getTitle();
            if("405".equals(title)){
                Thread.sleep(1000);
                browser.navigate().refresh();
                title = browser.getTitle();
                if("405".equals(title)) {
                    throw new Exception("被反扒");
                }
            }
        }

        // 获取比赛信息
        List<WebElement> contList = browser.findElements(By.className("cont"));
        for (WebElement cont : contList) {
            String dateStr = cont.getText().substring(0,10);
            log.warn("查找比赛日期：{}",startDate.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
            LocalDate ds = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
            if (!ds.equals(startDate)){
                continue;
            }
            WebElement changeDate = cont.findElement(By.cssSelector("#ChangeDate a span"));
            String changeDateText = changeDate.getText().split(" ")[0];
            LocalDate matchDate = LocalDate.parse(changeDateText, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));

            if (matchDate.equals(startDate)){
                    List<WebElement> touZhuList = cont.findElements(By.className("touzhu_1"));
                    for (int j = 0; j < touZhuList.size(); j++) {
                        WebElement webElement = touZhuList.get(j);
                        String realCs = webElement.findElement(By.xpath("./div[5]/div/p[1]")).getText();
                        if ("延期".equals(realCs)) {
                            continue;
                        }
                        try {
                            webElement.findElement(By.className("spfweik_1"));
                            log.warn("发现 停售等信息 进行下一场比赛");
                            continue;
                        } catch (Exception e) {
                            log.warn("未发现 停售等信息 继续进行");
                        }
                        String mid = null;
                        mid = webElement.getAttribute("data-mid");
                        System.out.println(mid);
                        WebElement more = webElement.findElement(By.cssSelector(".more"));
                        // 创建actions对象
                        // 获取js执行器
                        JavascriptExecutor javascriptExecutor = (JavascriptExecutor)browser;
                        javascriptExecutor.executeScript("arguments[0].scrollIntoView(true);", more);
//            javascriptExecutor.executeScript("window.scrollTo(0,400);");
                        javascriptExecutor.executeScript("arguments[0].click();", more);
                        ThreadUtil.safeSleep(500);




                        String mainTeamName = null;
                        String gustTeamName = null;
                        String rq = null;
                        String mainWin = null;
                        String level = null;
                        String gustWin = null;
                        String rqMainWin = null;
                        String rqLevel = null;
                        String rqGustWin = null;
                        String mainWin10 = null;
                        String mainWin20 = null;
                        String mainWin21 = null;
                        String mainWin30 = null;
                        String mainWin31 = null;
                        String mainWin32 = null;
                        String mainWin40 = null;
                        String mainWin41 = null;
                        String mainWin42 = null;
                        String mainWin50 = null;
                        String mainWin51 = null;
                        String mainWin52 = null;
                        String mainWinOther = null;
                        String level00 = null;
                        String level11 = null;
                        String level22 = null;
                        String level33 = null;
                        String levelOther = null;
                        String gustWin01 = null;
                        String gustWin02 = null;
                        String gustWin12 = null;
                        String gustWin03 = null;
                        String gustWin13 = null;
                        String gustWin23 = null;
                        String gustWin04 = null;
                        String gustWin14 = null;
                        String gustWin24 = null;
                        String gustWin05 = null;
                        String gustWin15 = null;
                        String gustWin25 = null;
                        String gustWinOther = null;
                        String jq0 = null;
                        String jq1 = null;
                        String jq2 = null;
                        String jq3 = null;
                        String jq4 = null;
                        String jq5 = null;
                        String jq6 = null;
                        String jqGt6 = null;

                        String ss = null;
                        String sp = null;
                        String sf = null;
                        String ps = null;
                        String pp = null;
                        String pf = null;
                        String fs = null;
                        String fp = null;
                        String ff = null;
                        String half = null;

                        // 主队名称
                        String mainTeamXpath = String.format("//*[@id=\"match_%s\"]/div[2]/div/div[1]/div/div[2]",mid);
                        mainTeamName = webElement.findElement(By.xpath(mainTeamXpath)).getText();
                        // 客队名称
                        String gustTeamXpath = String.format("//*[@id=\"match_%s\"]/div[2]/div/div[3]/div/div[3]",mid);
                        gustTeamName = webElement.findElement(By.xpath(gustTeamXpath)).getText();


                        rq = webElement.getAttribute("data-rq");
                        // 主队胜赔率
                        try {
                            mainWin = webElement.findElement(By.xpath("./div[2]/div/div[1]/div/div[3]")).getText();
                            level = webElement.findElement(By.xpath("./div[2]/div/div[2]/div/div")).getText();
                            gustWin = webElement.findElement(By.xpath("./div[2]/div/div[3]/div/div[1]")).getText();
                            rqMainWin = webElement.findElement(By.xpath("./div[4]/div/div[1]/div/div[2]")).getText();
                            rqLevel = webElement.findElement(By.xpath("./div[4]/div/div[2]/div/div")).getText();
                            rqGustWin = webElement.findElement(By.xpath("./div[4]/div/div[3]/div/div[1]")).getText();

                            WebElement moreMid = browser.findElement(By.id("more_" + mid));
                            List<WebElement> zk1 = moreMid.findElements(By.className("zk_1"));
                            for (int i = 0; i < zk1.size(); i++) {
                                WebElement zk = zk1.get(i);
                                if (i == 0) {
                                    mainWin10 = zk.findElement(By.xpath("./div[1]/div/div[2]")).getText();
                                    mainWin20 = zk.findElement(By.xpath("./div[2]/div/div[2]")).getText();
                                    mainWin21 = zk.findElement(By.xpath("./div[3]/div/div[2]")).getText();
                                    mainWin30 = zk.findElement(By.xpath("./div[4]/div/div[2]")).getText();
                                    mainWin31 = zk.findElement(By.xpath("./div[5]/div/div[2]")).getText();
                                    mainWin32 = zk.findElement(By.xpath("./div[6]/div/div[2]")).getText();
                                    mainWin40 = zk.findElement(By.xpath("./div[7]/div/div[2]")).getText();
                                    mainWin41 = zk.findElement(By.xpath("./div[8]/div/div[2]")).getText();
                                    mainWin42 = zk.findElement(By.xpath("./div[9]/div/div[2]")).getText();
                                    mainWin50 = zk.findElement(By.xpath("./div[10]/div/div[2]")).getText();
                                    mainWin51 = zk.findElement(By.xpath("./div[11]/div/div[2]")).getText();
                                    mainWin52 = zk.findElement(By.xpath("./div[12]/div/div[2]")).getText();
                                    mainWinOther = zk.findElement(By.xpath("./div[13]/div/div[2]")).getText();
                                } else if (i == 1) {
                                    level00 = zk.findElement(By.xpath("./div[1]/div/div[2]")).getText();
                                    level11 = zk.findElement(By.xpath("./div[2]/div/div[2]")).getText();
                                    level22 = zk.findElement(By.xpath("./div[3]/div/div[2]")).getText();
                                    level33 = zk.findElement(By.xpath("./div[4]/div/div[2]")).getText();
                                    levelOther = zk.findElement(By.xpath("./div[5]/div/div[2]")).getText();
                                } else if (i == 2) {
                                    gustWin01 = zk.findElement(By.xpath("./div[1]/div/div[2]")).getText();
                                    gustWin02 = zk.findElement(By.xpath("./div[2]/div/div[2]")).getText();
                                    gustWin12 = zk.findElement(By.xpath("./div[3]/div/div[2]")).getText();
                                    gustWin03 = zk.findElement(By.xpath("./div[4]/div/div[2]")).getText();
                                    gustWin13 = zk.findElement(By.xpath("./div[5]/div/div[2]")).getText();
                                    gustWin23 = zk.findElement(By.xpath("./div[6]/div/div[2]")).getText();
                                    gustWin04 = zk.findElement(By.xpath("./div[7]/div/div[2]")).getText();
                                    gustWin14 = zk.findElement(By.xpath("./div[8]/div/div[2]")).getText();
                                    gustWin24 = zk.findElement(By.xpath("./div[9]/div/div[2]")).getText();
                                    gustWin05 = zk.findElement(By.xpath("./div[10]/div/div[2]")).getText();
                                    gustWin15 = zk.findElement(By.xpath("./div[11]/div/div[2]")).getText();
                                    gustWin25 = zk.findElement(By.xpath("./div[12]/div/div[2]")).getText();
                                    gustWinOther = zk.findElement(By.xpath("./div[13]/div/div[2]")).getText();
                                } else if (i == 3) {
                                    jq0 = zk.findElement(By.xpath("./div[1]/div/div[2]")).getText();
                                    jq1 = zk.findElement(By.xpath("./div[2]/div/div[2]")).getText();
                                    jq2 = zk.findElement(By.xpath("./div[3]/div/div[2]")).getText();
                                    jq3 = zk.findElement(By.xpath("./div[4]/div/div[2]")).getText();
                                    jq4 = zk.findElement(By.xpath("./div[5]/div/div[2]")).getText();
                                    jq5 = zk.findElement(By.xpath("./div[6]/div/div[2]")).getText();
                                    jq6 = zk.findElement(By.xpath("./div[7]/div/div[2]")).getText();
                                    jqGt6 = zk.findElement(By.xpath("./div[8]/div/div[2]")).getText();
                                }else if(i == 4){
                                    ss = zk.findElement(By.xpath("./div[1]/div/div[2]")).getText();
                                    sp = zk.findElement(By.xpath("./div[2]/div/div[2]")).getText();
                                    sf = zk.findElement(By.xpath("./div[3]/div/div[2]")).getText();
                                    ps = zk.findElement(By.xpath("./div[4]/div/div[2]")).getText();
                                    pp = zk.findElement(By.xpath("./div[5]/div/div[2]")).getText();
                                    pf = zk.findElement(By.xpath("./div[6]/div/div[2]")).getText();
                                    fs = zk.findElement(By.xpath("./div[7]/div/div[2]")).getText();
                                    fp = zk.findElement(By.xpath("./div[8]/div/div[2]")).getText();
                                    ff = zk.findElement(By.xpath("./div[9]/div/div[2]")).getText();
                                }
                            }
                            if (StrUtil.isBlank(mainWin10) && StrUtil.isBlank(level00) && StrUtil.isBlank(gustWin01) && StrUtil.isBlank(jq0)) {
                                continue;
                            }
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }

                        int hour = LocalTime.now().getHour();
                        RealTimeMatch match = mapper.selectOne(Wrappers.<RealTimeMatch>lambdaQuery()
                                .eq(RealTimeMatch::getHour,hour)
                            .eq(RealTimeMatch::getMatchFlag, mid));
                        if(Objects.isNull(match)){
                            match = new RealTimeMatch();
                        }
                        match.setMatchFlag(mid);
                        match.setMatchDate(DateUtil.parse(matchDate.format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)),DatePattern.NORM_DATE_PATTERN));
                        match.setMainTeamName(mainTeamName);
                        match.setGustTeamName(gustTeamName);
                        match.setRq(rq);
                        match.setMainWinOdds(StrUtil.isNotBlank(mainWin)?new BigDecimal(mainWin) : BigDecimal.ZERO);
                        match.setLevelOdds(StrUtil.isNotBlank(level)?new BigDecimal(level) : BigDecimal.ZERO);
                        match.setGustWinOdds(StrUtil.isNotBlank(gustWin)?new BigDecimal(gustWin) : BigDecimal.ZERO);
                        match.setRqMainWinOdds(StrUtil.isNotBlank(rqMainWin)?new BigDecimal(rqMainWin) : BigDecimal.ZERO);
                        match.setRqLevelOdds(StrUtil.isNotBlank(rqLevel)?new BigDecimal(rqLevel) : BigDecimal.ZERO);
                        match.setRqGustWinOdds(StrUtil.isNotBlank(rqGustWin)?new BigDecimal(rqGustWin) : BigDecimal.ZERO);

                        match.setCs10(StrUtil.isNotBlank(mainWin10)?new BigDecimal(mainWin10) : BigDecimal.ZERO);
                        match.setCs20(StrUtil.isNotBlank(mainWin20)?new BigDecimal(mainWin20) : BigDecimal.ZERO);
                        match.setCs21(StrUtil.isNotBlank(mainWin21)?new BigDecimal(mainWin21) : BigDecimal.ZERO);
                        match.setCs30(StrUtil.isNotBlank(mainWin30)?new BigDecimal(mainWin30) : BigDecimal.ZERO);
                        match.setCs31(StrUtil.isNotBlank(mainWin31)?new BigDecimal(mainWin31) : BigDecimal.ZERO);
                        match.setCs32(StrUtil.isNotBlank(mainWin32)?new BigDecimal(mainWin32) : BigDecimal.ZERO);
                        match.setCs40(StrUtil.isNotBlank(mainWin40)?new BigDecimal(mainWin40) : BigDecimal.ZERO);
                        match.setCs41(StrUtil.isNotBlank(mainWin41)?new BigDecimal(mainWin41) : BigDecimal.ZERO);
                        match.setCs42(StrUtil.isNotBlank(mainWin42)?new BigDecimal(mainWin42) : BigDecimal.ZERO);
                        match.setCs50(StrUtil.isNotBlank(mainWin50)?new BigDecimal(mainWin50) : BigDecimal.ZERO);
                        match.setCs51(StrUtil.isNotBlank(mainWin51)?new BigDecimal(mainWin51) : BigDecimal.ZERO);
                        match.setCs52(StrUtil.isNotBlank(mainWin52)?new BigDecimal(mainWin52) : BigDecimal.ZERO);
                        match.setWinOther(StrUtil.isNotBlank(mainWinOther)?new BigDecimal(mainWinOther) : BigDecimal.ZERO);
                        match.setLevel00(StrUtil.isNotBlank(level00)?new BigDecimal(level00) : BigDecimal.ZERO);
                        match.setLevel11(StrUtil.isNotBlank(level11)?new BigDecimal(level11) : BigDecimal.ZERO);
                        match.setLevel22(StrUtil.isNotBlank(level22)?new BigDecimal(level22) : BigDecimal.ZERO);
                        match.setLevel33(StrUtil.isNotBlank(level33)?new BigDecimal(level33) : BigDecimal.ZERO);
                        match.setLevelOther(StrUtil.isNotBlank(levelOther)?new BigDecimal(levelOther) : BigDecimal.ZERO);
                        match.setCs01(StrUtil.isNotBlank(gustWin01)?new BigDecimal(gustWin01) : BigDecimal.ZERO);
                        match.setCs02(StrUtil.isNotBlank(gustWin02)?new BigDecimal(gustWin02) : BigDecimal.ZERO);
                        match.setCs12(StrUtil.isNotBlank(gustWin12)?new BigDecimal(gustWin12) : BigDecimal.ZERO);
                        match.setCs03(StrUtil.isNotBlank(gustWin03)?new BigDecimal(gustWin03) : BigDecimal.ZERO);
                        match.setCs13(StrUtil.isNotBlank(gustWin13)?new BigDecimal(gustWin13) : BigDecimal.ZERO);
                        match.setCs23(StrUtil.isNotBlank(gustWin23)?new BigDecimal(gustWin23) : BigDecimal.ZERO);
                        match.setCs04(StrUtil.isNotBlank(gustWin04)?new BigDecimal(gustWin04) : BigDecimal.ZERO);
                        match.setCs14(StrUtil.isNotBlank(gustWin14)?new BigDecimal(gustWin14) : BigDecimal.ZERO);
                        match.setCs24(StrUtil.isNotBlank(gustWin24)?new BigDecimal(gustWin24) : BigDecimal.ZERO);
                        match.setCs05(StrUtil.isNotBlank(gustWin05)?new BigDecimal(gustWin05) : BigDecimal.ZERO);
                        match.setCs15(StrUtil.isNotBlank(gustWin15)?new BigDecimal(gustWin15) : BigDecimal.ZERO);
                        match.setCs25(StrUtil.isNotBlank(gustWin25)?new BigDecimal(gustWin25) : BigDecimal.ZERO);
                        match.setLostOther(StrUtil.isNotBlank(gustWinOther)?new BigDecimal(gustWinOther) : BigDecimal.ZERO);
                        match.setJq0(StrUtil.isNotBlank(jq0)?new BigDecimal(jq0) : BigDecimal.ZERO);
                        match.setJq1(StrUtil.isNotBlank(jq1)?new BigDecimal(jq1) : BigDecimal.ZERO);
                        match.setJq2(StrUtil.isNotBlank(jq2)?new BigDecimal(jq2) : BigDecimal.ZERO);
                        match.setJq3(StrUtil.isNotBlank(jq3)?new BigDecimal(jq3) : BigDecimal.ZERO);
                        match.setJq4(StrUtil.isNotBlank(jq4)?new BigDecimal(jq4) : BigDecimal.ZERO);
                        match.setJq5(StrUtil.isNotBlank(jq5)?new BigDecimal(jq5) : BigDecimal.ZERO);
                        match.setJq6(StrUtil.isNotBlank(jq6)?new BigDecimal(jq6) : BigDecimal.ZERO);
                        match.setJqGt6(StrUtil.isNotBlank(jqGt6)?new BigDecimal(jqGt6) : BigDecimal.ZERO);
                        match.setSs(StrUtil.isNotBlank(ss)?new BigDecimal(ss) : BigDecimal.ZERO);
                        match.setSp(StrUtil.isNotBlank(sp)?new BigDecimal(sp) : BigDecimal.ZERO);
                        match.setSf(StrUtil.isNotBlank(sf)?new BigDecimal(sf) : BigDecimal.ZERO);
                        match.setPs(StrUtil.isNotBlank(ps)?new BigDecimal(ps) : BigDecimal.ZERO);
                        match.setPp(StrUtil.isNotBlank(pp)?new BigDecimal(pp) : BigDecimal.ZERO);
                        match.setPf(StrUtil.isNotBlank(pf)?new BigDecimal(pf) : BigDecimal.ZERO);
                        match.setFs(StrUtil.isNotBlank(fs)?new BigDecimal(fs) : BigDecimal.ZERO);
                        match.setFp(StrUtil.isNotBlank(fp)?new BigDecimal(fp) : BigDecimal.ZERO);
                        match.setFf(StrUtil.isNotBlank(ff)?new BigDecimal(ff) : BigDecimal.ZERO);
                        match.setHour(hour);
                        if(Objects.isNull(match.getId())){
                            mapper.insert(match);
                        }else {
                            mapper.updateById(match);
                        }
                    }
            }
        }
    }

    public boolean isExistElement(WebElement webElement, By by) {
        try {
            webElement.findElement(by);
            //可以把上边一行，换成下边注释掉的这一行，指定时间在dom树中没找到这个元素就报错
            //new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("")));
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
